package com.own.business.message.center.controller.user;

import com.own.business.message.center.business.service.MessageCenterContentService;
import com.own.business.message.center.business.service.MessageCenterGroupService;
import com.own.business.message.center.controller.user.entity.vo.MessageCenterTypeMapShowVo;
import com.own.business.message.center.entity.query.MessageCenterGroupQuery;
import com.own.business.message.center.entity.vo.MessageCenterGroupVo;
import com.own.component.common.base.controller.BaseController;
import com.own.component.common.login.util.SessionUserUtil;
import com.own.component.common.model.R;
import com.own.component.common.model.ResultModel;
import com.own.component.log.operation.aop.LogOperationAop;
import com.own.component.log.operation.aop.LogOperationModuleAop;
import com.own.constant.ConstantApiPrefix;
import com.own.constant.ConstantBeanSuffix;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserMessageCenterGroupController
 *
 * @author chenxueli
 * @date 2023-11-13 14:39:27
 */
@Tag(name = "消息中心分组" + ConstantBeanSuffix.USER_CLIENT + ConstantBeanSuffix.CONTROLLER)
@RestController
@LogOperationModuleAop("消息中心分组" + ConstantBeanSuffix.USER_CLIENT)
@RequestMapping(ConstantApiPrefix.BASE_API_V1_PREFIX + ConstantApiPrefix.USER_CLIENT + "message/center/group")
public class UserMessageCenterGroupController extends BaseController {

    @Resource
    private MessageCenterGroupService messageCenterGroupService;
    @Resource
    private MessageCenterContentService messageCenterContentService;

    @LogOperationAop
    @Operation(summary = "查询分组列表")
    @GetMapping("/search")
    public ResultModel<List<MessageCenterTypeMapShowVo>> map() {
        var loginUser = SessionUserUtil.getLoginUser();
        var query = new MessageCenterGroupQuery();
        query.setIsShow(1);
        var list = messageCenterGroupService.map(query);
        // 查询未读数量
        var unreadCountMap = messageCenterContentService.mapCountUnreadByUserIdForGroupKey(loginUser.userId());
        // 查询最新的消息
        var latestMessageMap = messageCenterContentService.mapLatestMessageByUserIdForGroupKey(loginUser.userId());
        // 整理数据
        var resultList = list.stream().map(item -> {
            var vo = new MessageCenterTypeMapShowVo(item);
            vo.setUnreadCount(unreadCountMap.getOrDefault(item.getGroupKey(), 0));
            vo.setLast(latestMessageMap.get(item.getGroupKey()));
            return vo;
        }).toList();
        return R.success(resultList);
    }

    @Operation(summary = "获取所有的分组信息（前端缓存用）")
    @GetMapping("/all")
    public ResultModel<List<MessageCenterGroupVo>> all() {
        var list = messageCenterGroupService.all(new MessageCenterGroupQuery());
        return R.success(list);
    }

}
